#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;
const int N = 1010;

int n;
int q[N];
unordered_map<int, int> mp[N];

void dfs(int idx, int num, int cnt){
    if(mp[idx].count(num)){
        mp[idx][num] = min(mp[idx][num], cnt);
        return;
    }else{
        mp[idx][num] = cnt;
    }

    if(num % 2 == 0){
        dfs(idx, num / 2, cnt + 1);
    }

    if(num % 3 == 0){
        dfs(idx, num / 3, cnt + 1);
    }
}


int main(){
#ifdef LOCALFLAG
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
#endif


    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n;
    for(int i = 1; i <= n; i ++) cin >> q[i];

    for(int i = 1; i <= n; i ++) dfs(i, q[i], 0);

    LL mi = 1e18;
    // cout << mp[1][1] << '\n';
    // cout << mp[2][1] << '\n';
    // cout << mp[3][1] << '\n';

    for(auto &[key, val] : mp[1]){
        bool ok = true;
        LL t = 0;
        for(int i = 1; i <= n; i ++){
            if(!mp[i].count(key)){
                ok = false;
                break;
            }

            t += mp[i][key];
        }
        // if(ok && t == 0) cout << "val: " << val << '\n';
        if(ok) mi = min(mi, t);
    }


    if(mi == 1e18) cout << -1 << '\n';
    else cout << mi << '\n';

    return 0;
}